﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
using System.Reflection;
using Handrise.BasicModal.Serialization;
using System.Configuration;

namespace Handrise.CDCable.Business
{
    /// <summary>
    /// 与主站服务器交互类
    /// </summary>
    public class ServerExchange
    {
        private ServerExchangeWebService.CommonWSServicePortTypeClient ws = new ServerExchangeWebService.CommonWSServicePortTypeClient();
        private string token = "" ;
        private bool useTestData = false;

        public ServerExchange()
        {
            try
            {
                useTestData = bool.Parse(ConfigurationSettings.AppSettings["UseTestData"]);
            }
            catch
            {
            }

        }
        
        /// <summary>
        /// 下载标准数据及设备
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="eqpUpdateTime"></param>
        public string DownloadStandardDataAndDevice(string userName, string eqpUpdateTime)
        {
            try
            {
                //设置参数
                List<MessageParameter> parameters = new List<MessageParameter>();
                parameters.Add(new MessageParameter("accountid", "String", userName));
                parameters.Add(new MessageParameter("eqpUpdateTime", "String", eqpUpdateTime));
                parameters.Add(new MessageParameter("isDownloadGIS", "Boolean", "true"));

                //获取发送给主站服务器的数据
                string sendData = CommonFunction.BuildSendData("pdaDownLoad", "iPDAPackageDataService", parameters, true);

                string re = "";
                if (useTestData)
                {
                    //测试数据
                    XmlDocument tempXmlDoc = new XmlDocument();
                    tempXmlDoc.Load(CommonFunction.GetLocalPath() + @"\DownloadDevice_" + userName + ".xml");
                    re = tempXmlDoc.InnerXml;

                }
                else
                {
                    //请求数据
                    LogService.SaveLog("下载标准数据、设备及设备经纬度", "发送数据：" + sendData);
                    re = ws.operationData(token, sendData);
                    LogService.SaveLog("下载标准数据、设备及设备经纬度", "接收数据：" + re);
                }
                return re; 

                ////测试数据
                //XmlDocument tempXmlDoc = new XmlDocument();
                //tempXmlDoc.Load(CommonFunction.GetLocalPath() + @"\lixiaohua_padDownLoad.xml");
                //return tempXmlDoc.InnerXml;
            }
            catch (Exception e)
            {
                LogService.SaveLog("下载标准数据、设备及设备经纬度", "异常：" + e.Message);

                return "";
            }

        }

        /// <summary>
        /// 登录系统
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public string Login(string userName, string password)
        {
            try
            {
                //设置参数
                List<MessageParameter> parameters = new List<MessageParameter>();
                parameters.Add(new MessageParameter("username", "String", userName));
                parameters.Add(new MessageParameter("password", "String", password));

                //获取发送给主站服务器的数据
                string sendData = CommonFunction.BuildSendData("pdaLogin", "iPdaLoginService", parameters, true);

                string re = "";
                if (useTestData)
                {
                    //测试数据
                    XmlDocument tempXmlDoc = new XmlDocument();
                    tempXmlDoc.Load(CommonFunction.GetLocalPath() + @"\Login_" + userName + ".xml");
                    re = tempXmlDoc.InnerXml;

                }
                else
                {
                    //向主站服务请求数据
                    LogService.SaveLog("系统登录", "发送数据：" + sendData);
                    re = ws.operationData(token, sendData);
                    LogService.SaveLog("系统登录", "接收数据：" + re);
                }

                return re;

            }
            catch (Exception e)
            {
                LogService.SaveLog("系统登录", "异常：" + e.Message);
              
                return "";
            }


        }

        /// <summary>
        /// 下载巡视计划（包含计划、计划设备、缺陷、指挥信息）
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public string DownloadPlan(string userName)
        {
            try
            {
                //设置参数
                List<MessageParameter> parameters = new List<MessageParameter>();
                parameters.Add(new MessageParameter("accountid", "String", userName));

                //获取发送给主站服务器的数据
                string sendData = CommonFunction.BuildSendData("pdaPlanDownLoad", "iPdaDownLoadService", parameters, true);

                string re = "";
                if (useTestData)
                {
                    //测试数据
                    XmlDocument tempXmlDoc = new XmlDocument();
                    tempXmlDoc.Load(CommonFunction.GetLocalPath() + @"\DownloadPlan_" + userName + ".xml");
                    re = tempXmlDoc.InnerXml;

                }
                else
                {
                    //请求数据
                    LogService.SaveLog("下载巡视计划", "发送数据：" + sendData);
                    re = ws.operationData(token, sendData);
                    LogService.SaveLog("下载巡视计划", "接收数据：" + re);
                }
                return re;

            }
            catch (Exception e)
            {
                LogService.SaveLog("下载巡视计划", "异常：" + e.Message);
                return "";
            }
        }

        /// <summary>
        /// 确认下载巡视计划完成
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="planIDs">下载的巡视计划ID，多个通过“,”分隔</param>
        /// <param name="communicationIDs">下载的指挥信息，多个通过“,”分隔</param>
        /// <returns></returns>
        public string ComfireDownloadPlan(string userName, string planIDs,string communicationIDs)
        {
            try
            {
                string comfireInfo = "<PdaDownLoadOK xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">" ;
        			   comfireInfo += "<accountid>" + userName + "</accountid>" ;
                       comfireInfo += "<planIds>" + planIDs + "</planIds>" ;
                       comfireInfo += "<pSmIds>" + communicationIDs + "</pSmIds>";
        		       comfireInfo += "</PdaDownLoadOK>";

                //设置参数
                List<MessageParameter> parameters = new List<MessageParameter>();
                parameters.Add(new MessageParameter("pdaDownLoadOK", "PdaDownLoadOK", comfireInfo));

                //获取发送给主站服务器的数据
                string sendData = CommonFunction.BuildSendData("pdaDownLoadOK", "iPdaDownLoadService", parameters, true);


                string re = "";
                if (useTestData)
                {
                    //测试数据
                    XmlDocument tempXmlDoc = new XmlDocument();
                    tempXmlDoc.Load(CommonFunction.GetLocalPath() + @"\ComfireDownloadPlan_" + userName + ".xml");
                    re = tempXmlDoc.InnerXml;

                }
                else
                {
                    //请求数据
                    LogService.SaveLog("确认下载巡视计划完成", "发送数据：" + sendData);
                    re = ws.operationData(token, sendData);
                    LogService.SaveLog("确认下载巡视计划完成", "接收数据：" + re);
                }
                return re;
            }
            catch (Exception e)
            {
                LogService.SaveLog("确认下载巡视计划完成", "异常：" + e.Message);
                
                return "";
            }
        }

        /// <summary>
        /// 上传计划状态
        /// </summary>
        /// <param name="planID"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public string UploadPlanState(string userName,string planID, string state)
        {
            try
            {
                //设置参数
                List<MessageParameter> parameters = new List<MessageParameter>();
                parameters.Add(new MessageParameter("planID", "String", planID));
                parameters.Add(new MessageParameter("state", "String", state));

                //获取发送给主站服务器的数据
                string sendData = CommonFunction.BuildSendData("pdaPlanStateUpload", "iPdaUploadService", parameters, true);


                string re = "";
                if (useTestData)
                {
                    //测试数据
                    XmlDocument tempXmlDoc = new XmlDocument();
                    tempXmlDoc.Load(CommonFunction.GetLocalPath() + @"\UploadPlanState_" + userName + ".xml");
                    re = tempXmlDoc.InnerXml;

                }
                else
                {
                    //请求数据
                    LogService.SaveLog("上传计划状态", "发送数据：" + sendData);
                    re = ws.operationData(token, sendData);
                    LogService.SaveLog("上传计划状态", "接收数据：" + re);
                }
                return re;

            }
            catch (Exception e)
            {

                LogService.SaveLog("上传计划状态", "异常：" + e.Message);
                return "";
            }
        }

        /// <summary>
        /// 上传缺陷
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="flaws">组织好的缺陷XML字符串</param>
        /// <returns></returns>
        public string UploadFlaws(string userName, string flaws)
        {
            try
            {
                string valueData = "<PdaMtdFlawinfoUpload xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"><loginName>" + userName + "</loginName>";
                valueData += flaws + "</PdaMtdFlawinfoUpload>";

                //设置参数
                List<MessageParameter> parameters = new List<MessageParameter>();
                parameters.Add(new MessageParameter("pmfu", "PdaMtdFlawinfoUpload", valueData));

                //获取发送给主站服务器的数据
                string sendData = CommonFunction.BuildSendData("pdaFlawUpload", "iPdaUploadService", parameters, true);

                string re = "";
                if (useTestData)
                {
                    //测试数据
                    XmlDocument tempXmlDoc = new XmlDocument();
                    tempXmlDoc.Load(CommonFunction.GetLocalPath() + @"\upload-success.xml");
                    re = tempXmlDoc.InnerXml;
                }
                else
                {
                    //请求数据
                    LogService.SaveLog("上传缺陷信息", "发送数据：" + sendData);
                    re = ws.operationData(token, sendData);
                    LogService.SaveLog("上传缺陷信息", "接收数据：" + re);
                }
                return re;
            }
            catch (Exception e)
            {
                LogService.SaveLog("上传缺陷信息", "异常：" + e.Message);
                return "";
            }
        }

        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="businessid">关联的业务ID</param>
        /// <param name="loginName">用户名</param>
        /// <param name="fileByte">文件数据（base64编码的字符串）</param>
        /// <param name="fileName">文件名称</param>
        /// <param name="fileType">文件类型：jpg,wav</param>
        /// <returns></returns>
        public string UploadFile(string userName,string businessid, string loginName, string fileByte, string fileName, string fileType)
        {
            try
            {
                string valueData = "<PdaFileTransfer xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">";
                valueData += "<businessid>" + businessid + "</businessid>" ;
                valueData += "<loginName>" + loginName + "</loginName>";
                valueData += "<fileByte>" + fileByte + "</fileByte>";
                valueData += "<fileName>" + fileName + "</fileName>";
                valueData += "<fileType>" + fileType + "</fileType>";
                valueData += "</PdaFileTransfer>";

                //设置参数
                List<MessageParameter> parameters = new List<MessageParameter>();
                parameters.Add(new MessageParameter("pdaFileTransfer", "PdaFileTransfer", valueData));

                //获取发送给主站服务器的数据
                string sendData = CommonFunction.BuildSendData("pdaFileUpload", "iPdaUploadService", parameters, true);

                string re = "";
                if (useTestData)
                {
                    //测试数据
                    XmlDocument tempXmlDoc = new XmlDocument();
                    tempXmlDoc.Load(CommonFunction.GetLocalPath() + @"\UploadFile_" + userName + ".xml");
                    re = tempXmlDoc.InnerXml;

                }
                else
                {
                    //请求数据
                    LogService.SaveLog("上传文件", "发送数据：" + sendData);
                    re = ws.operationData(token, sendData);
                    LogService.SaveLog("上传文件", "接收数据：" + re);
                }
                return re;

            }
            catch (Exception e)
            {
                LogService.SaveLog("上传文件", "异常：" + e.Message);
               
                return "";
            }
        }

        /// <summary>
        /// 上传轨迹信息
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="gpsTraces"></param>
        /// <returns></returns>
        public string UploadGPSTrace(string userName, string gpsTraces)
        {
            try
            {
                string valueData = "<GpsCurs xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">";
                valueData += gpsTraces + "</GpsCurs>";

                //设置参数
                List<MessageParameter> parameters = new List<MessageParameter>();
                parameters.Add(new MessageParameter("gpsCurs", "GpsCurs", valueData));

                //获取发送给主站服务器的数据
                string sendData = CommonFunction.BuildSendData("pdaGpsCursUpload", "iPdaUploadService", parameters, true);

                string re = "";
                if (useTestData)
                {
                    //测试数据
                    XmlDocument tempXmlDoc = new XmlDocument();
                    tempXmlDoc.Load(CommonFunction.GetLocalPath() + @"\UploadGPSTrace_" + userName + ".xml");
                    re = tempXmlDoc.InnerXml;

                }
                else
                {
                    //请求数据
                    LogService.SaveLog("上传轨迹信息", "发送数据：" + sendData);
                    re = ws.operationData(token, sendData);
                    LogService.SaveLog("上传轨迹信息", "接收数据：" + re);
                }
                return re;

                ////测试数据
                //XmlDocument tempXmlDoc = new XmlDocument();
                //tempXmlDoc.Load(CommonFunction.GetLocalPath() + @"\upload-success.xml");
                //return tempXmlDoc.InnerXml;

            }
            catch (Exception e)
            {
                LogService.SaveLog("上传轨迹信息", "异常：" + e.Message);
                return "";
            }
        }

        /// <summary>
        /// 上传指挥信息
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="communications"></param>
        /// <returns></returns>
        public string UploadCommunication(string userName, string communications)
        {
            try
            {
                string valueData = "<PdaFeedbackUpload xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">";
                valueData += communications + "</PdaFeedbackUpload>";

                //设置参数
                List<MessageParameter> parameters = new List<MessageParameter>();
                parameters.Add(new MessageParameter("mpu", "PdaFeedbackUpload", valueData));

                //获取发送给主站服务器的数据
                string sendData = CommonFunction.BuildSendData("pdaFeedbackUpload", "iPdaUploadService", parameters, true);


                string re = "";
                if (useTestData)
                {
                    //测试数据
                    XmlDocument tempXmlDoc = new XmlDocument();
                    tempXmlDoc.Load(CommonFunction.GetLocalPath() + @"\UploadCommunication_" + userName + ".xml");
                    re = tempXmlDoc.InnerXml;

                }
                else
                {
                    //请求数据
                    LogService.SaveLog("上传指挥信息", "发送数据：" + sendData);
                    re = ws.operationData(token, sendData);
                    LogService.SaveLog("上传指挥信息", "接收数据：" + re);
                }
                return re;




            }
            catch (Exception e)
            {
                LogService.SaveLog("上传指挥信息", "异常：" + e.Message);
                return "";
            }
        }

        /// <summary>
        /// 上传测温信息
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="tempratures">测温信息xml字符串</param>
        /// <returns></returns>
        public string UploadTemprature(string userName, string tempratures)
        {
            try
            {
                string valueData = "<PdaTemperatureUpload xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">";
                valueData += tempratures + "</PdaTemperatureUpload>";

                //设置参数
                List<MessageParameter> parameters = new List<MessageParameter>();
                parameters.Add(new MessageParameter("pdaTemperatureUpload", "PdaTemperatureUpload", valueData));

                //获取发送给主站服务器的数据
                string sendData = CommonFunction.BuildSendData("pdaTemperatureUpload", "iPdaUploadService", parameters, true);

                string re = "";
                if (useTestData)
                {
                    //测试数据
                    XmlDocument tempXmlDoc = new XmlDocument();
                    tempXmlDoc.Load(CommonFunction.GetLocalPath() + @"\UploadTemprature_" + userName + ".xml");
                    re = tempXmlDoc.InnerXml;

                }
                else
                {
                    //请求数据
                    LogService.SaveLog("上传测温信息", "发送数据：" + sendData);
                    re = ws.operationData(token, sendData);
                    LogService.SaveLog("上传测温信息", "接收数据：" + re);
                }
                return re;

            }
            catch (Exception e)
            {
                LogService.SaveLog("上传测温信息", "异常：" + e.Message);
                return "";
            }
        }

        /// <summary>
        /// 上传巡视记录
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="patrolLogs">巡视日志</param>
        /// <param name="patrolDetails">巡视详情</param>
        /// <returns></returns>
        public string UploadPatrolInfo(string userName,string patrolLogs,string patrolDetails)
        {
            try
            {
                string valueData = "<PdaPatrolsrecordUpload xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"><loginName>" + userName + "</loginName>";
                valueData += patrolLogs;
                valueData += patrolDetails + "</PdaPatrolsrecordUpload>";

                //设置参数
                List<MessageParameter> parameters = new List<MessageParameter>();
                parameters.Add(new MessageParameter("pud", "PDAUpload", valueData));

                //获取发送给主站服务器的数据
                string sendData = CommonFunction.BuildSendData("pdaPatrolsrecordUpload", "iPdaUploadService", parameters, true);

                string re = "";
                if (useTestData)
                {
                    //测试数据
                    XmlDocument tempXmlDoc = new XmlDocument();
                    tempXmlDoc.Load(CommonFunction.GetLocalPath() + @"\UploadPatrolInfo_" + userName + ".xml");
                    re = tempXmlDoc.InnerXml;

                }
                else
                {
                    //请求数据
                    LogService.SaveLog("上传巡视记录", "发送数据：" + sendData);
                    re = ws.operationData(token, sendData);
                    LogService.SaveLog("上传巡视记录", "接收数据：" + re);
                }
                return re;

            }
            catch (Exception e)
            {
                LogService.SaveLog("上传巡视记录", "异常：" + e.Message);
                return "";
            }
        }

        /// <summary>
        /// 下载指挥信息
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public string DownloadCommunication(string userName)
        {
            try
            {
                //设置参数
                List<MessageParameter> parameters = new List<MessageParameter>();
                parameters.Add(new MessageParameter("accountid", "String", userName));

                //获取发送给主站服务器的数据
                string sendData = CommonFunction.BuildSendData("pdaCommunicationsDown", "iPdaDownLoadService", parameters, true);

                string re = "";
                if (useTestData)
                {
                    //测试数据
                    XmlDocument tempXmlDoc = new XmlDocument();
                    tempXmlDoc.Load(CommonFunction.GetLocalPath() + @"\DownloadCommunication_" + userName + ".xml");
                    re = tempXmlDoc.InnerXml;

                }
                else
                {
                    //请求数据
                    LogService.SaveLog("下载指挥信息", "发送数据：" + sendData);
                    re = ws.operationData(token, sendData);
                    LogService.SaveLog("下载指挥信息", "接收数据：" + re);
                }
                return re;

            }
            catch (Exception e)
            {
                LogService.SaveLog("下载指挥信息", "异常：" + e.Message);
                return "";
            }
        }

        /// <summary>
        /// 获取令牌
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public string GetToken(string userName, string password)
        {
            try
            {
                return ws.getToken(userName, password);
            }
            catch (Exception e)
            {

                return "";
            }
        }



 
    }
}
